﻿using System;
using System.Collections.Generic;
using System.Text;
using TerminationCall.Core;
using System.Drawing;

namespace TerminationCall.Editor.Logic {

    public class EditorToolManager {

        public EditorTool EditorTool { get; set; }

        public EditorToolManager(EditorTool editorTool) {
            EditorTool = editorTool;
        }

        public Field GetSingleField(int X, int Y) {
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            return layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
        }

        public PaintTile SetNullTile(int X, int Y) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, null);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile SetNullTile(int X, int Y, TileFields fieldTile) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, null, fieldTile);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile SetSingleTile(int X, int Y, Tile tile) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, tile);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile SetSingleTile(int X, int Y, Tile tile, TileFields fieldTile) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, tile, fieldTile);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile SetChoosenSingleTile(int X, int Y) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, EditorTool.ChoosenTileSet.Tiles[EditorTool.ChoosenTiles.X, EditorTool.ChoosenTiles.Y]);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile SetChoosenSingleTile(int X, int Y, TileFields fieldTile) {
            PaintTile paintTile = CreatePaintTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y);
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);
            layerManager.SetTile(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y, EditorTool.ChoosenTileSet.Tiles[EditorTool.ChoosenTiles.X, EditorTool.ChoosenTiles.Y], fieldTile);
            SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + EditorTool.Offset.X, Y + EditorTool.Offset.Y));
            return paintTile;
        }

        public PaintTile[] SetChoosenTiles(int X, int Y) {

            List<PaintTile> paintTiles = new List<PaintTile>();

            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);

            for (int x = 0; x < EditorTool.ChoosenTiles.Width; x++) {
                for (int y = 0; y < EditorTool.ChoosenTiles.Height; y++) {
                    if (Y + y + EditorTool.Offset.Y < 17 && Y + y + EditorTool.Offset.Y >= 0 && X + x >= 0) {
                        PaintTile paintTile = CreatePaintTile(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y);
                        paintTiles.Add(paintTile);
                        layerManager.SetTile(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y, EditorTool.ChoosenTileSet.Tiles[EditorTool.ChoosenTiles.X + x, EditorTool.ChoosenTiles.Y + y]);
                        SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y));
                    }
                }
            }

            return paintTiles.ToArray();
        }

        public PaintTile[] SetChoosenTiles(int X, int Y, TileFields fieldTile) {

            List<PaintTile> paintTiles = new List<PaintTile>();

            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);

            for (int x = 0; x < EditorTool.ChoosenTiles.Width; x++) {
                for (int y = 0; y < EditorTool.ChoosenTiles.Height; y++) {
                    if (Y + y + EditorTool.Offset.Y < 17 && Y + y + EditorTool.Offset.Y >= 0 && X + x >= 0) {
                        PaintTile paintTile = CreatePaintTile(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y);
                        paintTiles.Add(paintTile);
                        layerManager.SetTile(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y, EditorTool.ChoosenTileSet.Tiles[EditorTool.ChoosenTiles.X + x, EditorTool.ChoosenTiles.Y + y], fieldTile);
                        SetNewTileFieldToPaintTile(paintTile, layerManager.GetField(X + x + EditorTool.Offset.X, Y + y + EditorTool.Offset.Y));
                    }
                }
            }

            return paintTiles.ToArray();
        }

        private void SetNewTileFieldToPaintTile(PaintTile paintTile, Field field) {
            if (field != null) {
                paintTile.NewTileField = field.TileField.Clone() as TileField;
            }
        }

        private PaintTile CreatePaintTile(int X, int Y) {
            LayerManager layerManager = new LayerManager(EditorTool.ChoosenLayer);

            PaintTile paintTile = new PaintTile(EditorTool.ChoosenLayer, this);

            Field field = layerManager.GetField(X, Y);

            if (field != null) {
                paintTile.OldTileField = field.TileField.Clone() as TileField;
            }

            paintTile.Point = new Point(X, Y);

            return paintTile;
        }

    }

}
